<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    title="查看报工"
    style="min-width: 1200px"
    draggable
    @close="closeDialog"
  >
    <!--<div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="生产报工单编号">
          <el-input
            v-model="searchParam.produceReportCode"
            placeholder="请输入生产报工单编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="周转卡编号">
          <el-input
            v-model="searchParam.produceTransferFormCode"
            placeholder="请输入周转卡编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="派工单编号">
          <el-input
            v-model="searchParam.workingFormCode"
            placeholder="请输入派工单编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="工作中心编号">
          <el-input
            v-model="searchParam.workCenterCode"
            placeholder="请输入工作中心编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>-->

    <el-card shadow="never" class="table-container">
      <!--<template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template>-->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        style="height: 450px;"
      >
        <!--<el-table-column type="selection" width="55" align="center" />-->
        <!--<el-table-column
          label="生产报工单编号"
          prop="produceReportCode"
          min-width="100"
          show-overflow-tooltip
        />-->
        <el-table-column
          label="周转卡编号"
          prop="produceTransferFormCode"
          min-width="120"
          show-overflow-tooltip
        />
        <!--<el-table-column
          label="派工单编号"
          prop="workingFormCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工作中心编号"
          prop="workCenterCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="线边库名称"
          prop="produceStorageName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工序id"
          prop="processId"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="物料id"
          prop="materialId"
          min-width="100"
          show-overflow-tooltip
        />-->
        <el-table-column
          label="物料编号"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("MeasurementUnit", scope.row.quantityUnit) }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否合格"
          prop="isValid"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("abnormalReportType", String(scope.row.isValid)) }}
          </template>
        </el-table-column>
        <el-table-column
          label="数量"
          prop="productQuantity"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <!--<el-table-column
          label="开工时间"
          prop="startTime"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="班次名称"
          prop="shiftName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="班组编号"
          prop="crewCode"
          min-width="100"
          show-overflow-tooltip
        />-->
        <el-table-column
          label="生产批次"
          prop="produceBatch"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="报工时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="操作人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <!--<el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>-->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
    <mesproducereportrecord-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </el-dialog>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "PRODUCEREPORTRECORDPAGE",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesproducereportrecord/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};
const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  readvisible: {
    type: Number,
    default: () => {
      return null;
    },
  },
});
// 初始化搜索参数数据
const searchParamInit = {
  produceReportCode: "",
  produceTransferFormCode: "",
  workingFormCode: "",
  workingFormId: props.data.id,
  workCenterCode: "",
};
const visibleDialog = ref(false);
watch(
  () => props.data,
  (newVal: any) => {
    visibleDialog.value = props.visible;
    if (newVal.id) {
      searchParamInit.workingFormId = newVal?.id;
      searchParam.value.workingFormId = newVal?.id;
      getTableList();
    }
  }
);
const deleteParam = {
  api: "/mes/mesproducereportrecord/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {

});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesproducereportrecord/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开生产报工表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改生产报工";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增生产报工";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开生产报工表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "生产报工详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/mes/mesproducereportrecord/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  reset();
}
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
